package com.aek.aekdroid;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aek.aekdroid.data.IDbConstants;
import com.aek.aekdroid.data.MatchData;

public class AddElevenActivity extends BaseActivity {

	public static final String MATCH_ID = "MATCH_ID";
	private Button ok;
	private Button cancel;
	private TextView match;
	private LinearLayout players;
	private int matchId = -1;
	private String matchFullTitle = "";
	String[] eleven = new String[11];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_eleven);
		match = (TextView) findViewById(R.id.add_eleven_matchFullTitle);
		ok = (Button) findViewById(R.id.add_eleven_ok);
		cancel = (Button) findViewById(R.id.add_eleven_cancel);
		ok.setOnClickListener(new ClickListener());
		cancel.setOnClickListener(new ClickListener());
		players = (LinearLayout) findViewById(R.id.add_eleven_playersLine);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			matchId = bundle.getInt(MATCH_ID, -1);
			matchFullTitle = aek.getMatchData().getMatchFullTitle(matchId);
			String el = aek.getMatchData().getMatchEleven(matchId);
			if (el != null) {
				eleven = el.split(",");
			}
		}
		match.setText(matchFullTitle);
		createList();
	}

	@Override
	public void onBackPressed() {
		Intent showMatchIntent = new Intent(this, ShowMatchActivity.class);
		showMatchIntent.putExtra(IDbConstants.MATCHES_C_ID, matchId);
		startActivity(showMatchIntent);
	}

	private void createList() {
		String[] list = MatchData.players.get(aek.getPeriod());
		if (list != null) {
			for (int i = 0; i < list.length; i++) {
				String name = list[i];
				CheckBox c = new CheckBox(this);
				c.setId(i);
				c.setText(name);
				c.setChecked(Helper.isInArray(name, eleven));
				c.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
				c.setBackgroundColor(i % 2 == 0 ? getResources().getColor(R.color.alternate_1) : getResources().getColor(
						R.color.alternate_2));
				c.setTextColor(i % 2 == 0 ? getResources().getColor(R.color.alternate_1_font) : getResources().getColor(
						R.color.alternate_2_font));
				players.addView(c);
			}
		}

	}

	class ClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.add_eleven_ok) {
				AddElevenActivity.this.submit();
			} else if (v.getId() == R.id.add_eleven_cancel) {
				finish();
			}

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	public void submit() {
		String pass = aek.getPassword();
		String query = "&pass=" + pass + "&match_id=" + this.matchId + "&eleven=" + eleven.toString();

		String url = WebAekDroid.ADD_ELEVEN_URL + query;
		MyLog.d(TAG, "Submiting eleven " + url);
		Helper.adminFunction(this, url);

	}

}
